/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : TimePatternHandler.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Jan 22, 2009  Raghu Sarraff			       Created
 */
package net.lwf.handler.time;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;

import lwf.handler.IProcessHandler;
import lwf.handler.ProcessHandler;
import lwf.handler.entities.RadioData;
import lwf.handler.helper.DisplayFormatter;
import net.lwf.common.constant.ICommonConstant;
import net.lwf.entity.QuestionInfo;
import net.lwf.handler.constant.IHandlerConstant;
import net.lwf.handler.time.util.Time;
import net.lwf.handler.time.util.TimeGenerator;
import net.lwf.handler.time.util.TimeUtil;
import net.lwf.number.util.RandomNumberGenerator;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;

//http://www.emanueleferonato.com/2010/12/11/javascript-analog-clock-with-no-images-and-no-css/
//http://randomibis.com/coolclock/
//http://www.webdeveloperjuice.com/2010/02/07/7-wonderful-analog-and-digital-clocks-using-css-and-javascript/

public class TimePatternHandler extends ProcessHandler implements
		IProcessHandler {

	private int nPatternTimes = 5;     // Number of time images for display
	
	public TimePatternHandler() {

	}
	
	
	/**
	 * @see net.lwf.handler.IProcessHandler#getDisplayFormatter()
	 */
	public DisplayFormatter getDisplayFormatter() {
		return null;
	}

	/**
	 * @see net.lwf.handler.IProcessHandler#getQuestionDesc()
	 */
	public String getQuestionDesc() {
		return null;
	}

	
	private String generateHtmlQuestion(String[]  patterntimes, String[] imagepaths) {
		StringBuffer sbQBlock = new StringBuffer();
		sbQBlock.append("<table border=\"0\" width=\"100%\" align=\"center\" cellspacing=\"2\" cellpadding=\"0\">");
			
        	sbQBlock.append("<tr>");
        	    for(int i=0; i<imagepaths.length; i++) {
        	    	if(i != 4) {
               			sbQBlock.append("<td width=\"17%\" class=\"datacenter\" nowrap>");
               				sbQBlock.append("<div class=\"time_clock\" >");
       							sbQBlock.append("<img src=\"" + imagepaths[i] + "\" >");
       						sbQBlock.append("</div>");
       						sbQBlock.append("<br>");
       						sbQBlock.append(patterntimes[i]);
       					sbQBlock.append("</td>");        
        	    	}
        	    	else {
               			sbQBlock.append("<td width=\"17%\" class=\"qBcenter\" nowrap>");
       					sbQBlock.append(imagepaths[i]);
       					sbQBlock.append("</td>");
        	    	}
        	    	sbQBlock.append("<td width=\"3%\" > &nbsp; </td>");
        	    }

        	sbQBlock.append("</tr>");
			sbQBlock.append("<tr>");
				sbQBlock.append("<td colspan=\"10\"> &nbsp; </td>");
			sbQBlock.append("</tr>");
			sbQBlock.append("<tr>");
				sbQBlock.append("<td colspan=\"10\"> &nbsp; </td>");
			sbQBlock.append("</tr>");
        sbQBlock.append("</table>");
        
        return sbQBlock.toString();	
	}
	
	//------------------------------------------------ Methods for PDF generation on the browser
	public void generate(Document document) {
	
	}

	@Override
	public List<QuestionInfo> generateMultiple() {
		return null;
	}

	@Override
	public QuestionInfo generateSingle() {
		return this.generateSingle(false);
	}
	
	private QuestionInfo generateSingle(boolean isMultiple) {
		QuestionInfo qinfo = new QuestionInfo();
		String[]  patterntimes = new String[nPatternTimes];
		String[] imagepaths = new String[nPatternTimes];
		
		int multiple = RandomNumberGenerator.generate(1, IHandlerConstant.EASY_MULTIPLES.length);
		int minutemultiple = IHandlerConstant.EASY_MULTIPLES[multiple-1];
		
		Time time = TimeGenerator.generateHMM(minutemultiple);
		
		for(int i=0; i<nPatternTimes; i++) {
			patterntimes[i] = time.getFormattedTime(); //in the format hh:mm a; ex. 11:45 PM
			imagepaths[i] = time.getImagePath();
			time.add(0, minutemultiple, 0);
		}
		String answer = patterntimes[nPatternTimes-1];
		if(answer.startsWith("0")) {
			answer = answer.substring(1);
			qinfo.setAnswer(answer);
		}
		imagepaths[nPatternTimes-1] = "?";
		qinfo.setHtmlQuestion(this.generateHtmlQuestion(patterntimes, imagepaths));
		qinfo.setPlaceHolder("Read the time pattern below carefully and identify missing value.");
		if(!isMultiple) {
			qinfo.setRadioAnswerChoices(this.getRadioAnswers(answer));	
		}
		return qinfo;
	}
	
	private List<RadioData> getRadioAnswers(String answer) {
		//11:45 PM
		List<RadioData> rdata = new ArrayList<RadioData>();
		rdata.add(new RadioData(answer, answer));
		StringTokenizer stk = new StringTokenizer(answer, " ");
		StringTokenizer stk_time = new StringTokenizer(stk.nextToken(), ICommonConstant.COLON);
		
		int hour = Integer.parseInt(stk_time.nextToken());
		int minute = Integer.parseInt(stk_time.nextToken());
		
		List<Integer> hour_choices = RandomNumberGenerator.generateIntsWithVariance(hour, 12, 3, 3);
		List<Integer> minute_choices = RandomNumberGenerator.generateIntsWithVariance(minute, 60, 3, 3);
		
		for(int i=0; i<hour_choices.size(); i++) {
			String answer_choice = TimeUtil.getFormattedTime(hour_choices.get(i), minute_choices.get(i), 0);
			rdata.add(new RadioData(answer_choice, answer_choice));
		}
		
		Collections.shuffle(rdata);
		
		return rdata;
	}
	


	@Override
	public int getColumns() {
		return 0;
	}

	@Override
	public void getContent(Document document) throws DocumentException {
	}

	@Override
	public int getRows() {
		return 0;
	}

	@Override
	public boolean isPortrait() {
		return false;
	}
	@Override
	public String getPdfInstructionalText() {
		return null;
	}

	@Override
	public String getPdfNote() {
		return null;
	}
}

